﻿using BaiTu.IOCServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiTu.DAL
{
    /// <summary>
    /// 数据操作实现类的抽象基类
    /// </summary>
    public class Repository<T> : IDAL.IRepository<T>
        where T : Domains.BaseDomain
    {

        private IDAL.IBaseRepository<T> _repository;
        /// <summary>
        /// 仓储类
        /// </summary>
        public IDAL.IBaseRepository<T> RepositoryDAL
        {
            get
            {
                if (_repository == null)
                {
                    _repository = EngineContext.Current.Resolve<IDAL.IBaseRepository<T>>();
                }
                return _repository;
            }
        }

        public Repository()
        {
            _repository = EngineContext.Current.Resolve<IDAL.IBaseRepository<T>>();
        }
        public Repository(IDAL.IBaseRepository<T> repository)
        {
            this._repository = repository;
        }
        public Framework.InnerResult Add(T t, bool useTransaction = false)
        {
            return _repository.Add(t,useTransaction);
        }

        public Framework.InnerResult Delete(T t, bool useTransaction = false)
        {
            return _repository.Delete(t,useTransaction);
        }

        public Framework.InnerResult Update(T t, bool useTransaction = false)
        {
            return _repository.Update(t,useTransaction);
        }

        public T GetById(string id)
        {
            return _repository.GetById(id);
        }

        public IQueryable<T> Table
        {
            get { return _repository.Table; }
        }

        public IQueryable<T> TableNoTracking
        {
            get { return _repository.TableNoTracking; }
        }


        public Framework.InnerResult Add(IEnumerable<T> list, bool useTransaction = false)
        {
            return _repository.Add(list,useTransaction);
        }

        public Framework.InnerResult Delete(IEnumerable<T> list, bool useTransaction = false)
        {
            return _repository.Delete(list,useTransaction);
        }

        public Framework.InnerResult Update(IEnumerable<T> list, bool useTransaction = false)
        {
            return _repository.Update(list,useTransaction);
        }

        public IEnumerable<T> Query(Func<T, bool> predicate)
        {
            return _repository.Query(predicate);
        }

        public IList<TEntity> ExecuteStoredProcedureList<TEntity>(string commandText, params object[] parameters) where TEntity : Domains.BaseDomain, new()
        {
            return _repository.ExecuteStoredProcedureList<TEntity>(commandText, parameters);
        }

        public IEnumerable<TElement> SqlQuery<TElement>(string sql, params object[] parameters)
        {
            return _repository.SqlQuery<TElement>(sql, parameters);
        }

        public int ExecuteSqlCommand(string sql, bool doNotEnsureTransaction = false, int? timeout = null, params object[] parameters)
        {
            return _repository.ExecuteSqlCommand(sql, doNotEnsureTransaction, timeout, parameters);
        }


        public Framework.InnerResult Delete(Func<T, bool> predicate, bool useTransaction = false)
        {
            return _repository.Delete(predicate,useTransaction);
        }

        public bool IsCommited
        {
            get { return _repository.IsCommited; }
        }

        public Framework.InnerResult Commit()
        {
            return _repository.Commit();
        }

        public Framework.InnerResult RollBack()
        {
            return _repository.RollBack();
        }
    }
}
